﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using NPOI.SS.Formula.Functions;

namespace SheepCommon.API.Vendors
{
    public class ClientCreateHander : IRequestHandler<ClientCreateCommand, Result>
    {
        private readonly IBaseRepository<Client> clientRepository;
        private readonly IMapper _mapper;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="vendorRepository"></param>
        /// <param name="mapper"></param>
        public ClientCreateHander(IBaseRepository<Client> clientRepository, IMapper mapper)
        {
            this.clientRepository = clientRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 客户新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ClientCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var gpprice = _mapper.Map<Client>(request);
                var list = await clientRepository.Add(gpprice);
                if (list > 0)
                {
                    gpprice.CreatedOwnerId = "张三";
                    gpprice.CreatedTime = DateTime.Now;
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
                else
                {
                    result.Msg = "添加失败";
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
